package com.yanqu.road.server.cmd.activity.newyearbag;

import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.NewYearBagProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.newyearbag.NewYearBagModule;
import com.yanqu.road.server.manger.activity.newyearbag.NewYearBagMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

@Cmd(code = GameProtocol.S_NEW_YEAR_BAG_OPEN, desc = "")
public class NewYearBagOpenCmd extends AbstractCommand {

    private int num;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        NewYearBagProto.NewYearBagOpenReqMsg reqMsg = NewYearBagProto.NewYearBagOpenReqMsg.parseFrom(packet.getBytes());
        num = reqMsg.getNum();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if (!NewYearBagMgr.inOpenTime()) {
            ret = GameErrorCode.E_NEW_YEAR_BAG_NO_IN_OPEN_TIME;
            return;
        }

        ret = player.getModule(NewYearBagModule.class).openByCmd(num);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            NewYearBagProto.NewYearBagOpenRespMsg.Builder builder = NewYearBagProto.NewYearBagOpenRespMsg.newBuilder();
            builder.setRet(ret);
            player.sendPacket(ClientProtocol.U_NEW_YEAR_BAG_OPEN, builder);
        }
    }
}
